package com.bosssoft.goods.controller;

import com.bosssoft.base.entity.Result;
import com.bosssoft.goods.domain.vo.AddCategoryVO;
import com.bosssoft.goods.domain.vo.EditCategoryVO;

import com.bosssoft.goods.service.GoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 商品分类API接口
 * @author Fang_Sir
 * @date 2021-04-12
 */
@RestController
@RequestMapping("/manage/category")
@Api(tags = "1-商品分类管理")
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @GetMapping("/getCategoryList")
    @ApiOperation(value = "获取商品分类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", defaultValue = "1", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "页大小", defaultValue = "10",dataType = "int"),
    })
    public Result<?> getCategoryList(@RequestParam(required = false,defaultValue = "0") int page,
                                     @RequestParam(required = false,defaultValue = "10")int size) {
        return Result.ok(goodsCategoryService.getCategoryList(page,size));
    }

    @PostMapping("/newCategory")
    @ApiOperation(value = "添加分类")
    public Result<?> addCategory(@Valid @RequestBody AddCategoryVO info, BindingResult result) {
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        if(goodsCategoryService.addCategory(info)){
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @DeleteMapping("/delCategory/{level}/{id}")
    @ApiOperation(value = "删除分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "分类层级", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "id", value = "分类编号", required = true, dataType = "int", paramType = "path"),
    })
    public Result<?> delCategory(@PathVariable("level") int level,@PathVariable("id") int id) {
        if(goodsCategoryService.delCategory(level, id)){
            return Result.ok();
        }
        return Result.error();
    }

    @PutMapping("/editCatName")
    @ApiOperation(value = "更改分类名称")
    public Result<?> editCategoryName(@Valid @RequestBody EditCategoryVO info, BindingResult result) {
        if(result.hasErrors()){
            return Result.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        try {
            if (goodsCategoryService.editCategoryName(info)) {
                return Result.ok();
            }
        }catch (Exception e){
            return Result.error(e.getMessage());
        }
        return Result.error();
    }

    @GetMapping("/isExistCategory")
    @ApiOperation(value = "商品分类是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "level", value = "分类层级", required = true, dataType = "long"),
            @ApiImplicitParam(name = "name", value = "分类名", required = true, dataType = "String"),
    })
    public Result<?> addCategory(@Parameter(description = "分类层级")int level,
                                 @Parameter(description = "分类名")String name) {
        boolean isExist = goodsCategoryService.isCategoryExist(level, name);
        return Result.ok(isExist);
    }
}
